package com.xd.study.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xd.study.R;

/**
 * Created by DGF on 2015/5/6.
 */
public class SystemServiceActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.sys_service);

        /**
         * 使用系统服务 LAYOUT_INFLATER_SERVICE 加载布局
         */
        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.sys_service, null);
        setContentView(view);

        /**
         * 使用系统服务 CONNECTIVITY_SERVICE 判断网络是否连接
         */
        Button btnConnectivity = (Button)findViewById(R.id.sys_service_btn_connectivity);
        btnConnectivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
                if (connectivityManager == null){
                    Toast.makeText(SystemServiceActivity.this, "无网络连接", Toast.LENGTH_SHORT).show();
                }else{
//                    NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
//                    for (NetworkInfo networkInfo : networkInfos){
//                        Log.i("SystemService", networkInfo.getTypeName()+": "+networkInfo.getDetailedState().toString());
//                    }
                    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    if (networkInfo.isConnected()){
                        Toast.makeText(SystemServiceActivity.this, "WIFI已连接", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(SystemServiceActivity.this, "有网络连接但WIFI未连接", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        /**
         * WIFI_SERVICE 获取和设置WIFI
         */
        Button btnWifi = (Button)findViewById(R.id.sys_service_btn_wifi);
        btnWifi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
                if (wifiManager.isWifiEnabled()){
                    wifiManager.setWifiEnabled(false);
                    Toast.makeText(SystemServiceActivity.this, "关闭wifi", Toast.LENGTH_SHORT).show();
                }else{
                    wifiManager.setWifiEnabled(true);
                    Toast.makeText(SystemServiceActivity.this, "打开wifi", Toast.LENGTH_SHORT).show();
                }
            }
        });

        /**
         * AUDIO_SERVICE 获取系统音量
         */
        Button btnVoice = (Button)findViewById(R.id.sys_service_btn_voice);
        btnVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
                int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
                int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
                Toast.makeText(SystemServiceActivity.this, "最大音量为："+maxVolume+" 当前音量："+currentVolume, Toast.LENGTH_SHORT).show();
            }
        });

        /**
         * ACTIVITY_SERVICE 获取应用程序的包名
         */
        Button btnActivity = (Button)findViewById(R.id.sys_service_btn_activity);
        btnActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
                String packageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
                Toast.makeText(SystemServiceActivity.this, packageName, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
